//
//  HomeCollectionVC.swift
//  SwiftPhophotoBrowser
//
//  Created by xmg on 16/8/20.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

private let reuseIdentifier = "HomeCell"

class HomeCollectionVC: UICollectionViewController {
    
    /** 懒加载商品 */
    private lazy var shops : [CommodityDisplay] = [CommodityDisplay]()
    
    /** 转场代理  */
    private lazy var transitionAnimation : PhotoBrowserAnimator = PhotoBrowserAnimator()
    override func viewDidLoad() {
        super.viewDidLoad()
     
        loadHomeData(0)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
    }

 

   
   
}

extension HomeCollectionVC {
    // MARK: UICollectionViewDataSource
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return shops.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HomeViewCell
        cell.shop = shops[indexPath.item]
        /** 设置cell刷新 */
        if indexPath.item == shops.count - 1{
            loadHomeData(shops.count);
        }
        return cell
    }

    // MARK: - UICollectionViewDelegate
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let photoBrowserVc = PhotoBrowserVC()
        photoBrowserVc.shops = shops
        photoBrowserVc.indexPath = indexPath
        
        /** 自定义转场动画 */
        photoBrowserVc.transitioningDelegate  = transitionAnimation
        transitionAnimation.indexPath = indexPath
        transitionAnimation.presentedDelegate = self
        transitionAnimation.dismissedDelegate = photoBrowserVc
        
        /** 设置跳转样式 */
        photoBrowserVc.modalPresentationStyle = .Custom
        /** 弹出控制器 */
        presentViewController(photoBrowserVc, animated: true, completion: nil)
    }
}

extension HomeCollectionVC {

    private func loadHomeData (offset : Int){
    
        /** 获取请求urlstring */
        let urlString = "http://mobapi.meilishuo.com/2.0/twitter/popular.json"
        /** 获取请求的参数 */
        let parameters = [
            "offset" : "\(offset)",
            "limit" : "30",
            "access_token" : "b92e0c6fd3ca919d3e7547d446d9a8c2"]
        
        NetworkTools.shareInstance.request(.GET, urlString: urlString, parameters: parameters) { (progress, result, error) -> () in
            /** 判断值效验 */
            if progress != nil {return}
            /** 错误校验 */
            if error != nil {return}
            
            /** 解析数据 */
            guard let result = result as? [String : NSObject] else{return}
            
            guard let dataArray = result["data"] as? [[String : NSObject]] else{return}
            /** 获取字典数组 */
            for dict in dataArray{
                self.shops.append(CommodityDisplay(dict: dict));
            }
            /** 刷新表格 */
            self.collectionView?.reloadData()
            
        }
    }
    
}

extension HomeCollectionVC : AnimatorPresentedDelegate {
    
    func startRect(indexPath : NSIndexPath) -> CGRect{
        /** 获取cell */
        guard let cell = collectionView?.cellForItemAtIndexPath(indexPath) else{return CGRectZero}
        /** 获取cell的frame */
        let cellFrame = cell.frame
        let startRect = collectionView?.convertRect(cellFrame, toCoordinateSpace: UIApplication.sharedApplication().keyWindow!)
        return startRect!
    }
    func imageView(indexPath : NSIndexPath) -> UIImageView{
        let imageView = UIImageView()
        /** 获取cell */
        guard let cell = collectionView?.cellForItemAtIndexPath(indexPath) as? HomeViewCell else {return imageView}
        /** 获取image */
        guard let image = cell.imageView.image else {return imageView}
        /** 设置imageView的属性 设置图片填充 裁剪 */
        imageView.image = image
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }
    func endRect(indexPath : NSIndexPath) -> CGRect{
        /** 获取cell */
        guard let cell = collectionView?.cellForItemAtIndexPath(indexPath) as? HomeViewCell else{return CGRectZero}
        /** 获取image */
        guard let image = cell.imageView.image else{return CGRectZero}
        
        return calculatePictureSizeWithImage(image)
        
    }
}

